<script>
	import {
		appInit,
		login
	} from "@/api/index.js"
	export default {
		data() {
			return {
				token: null,
				userInfo: {},
				isInitialized: false
			}
		},
		onLaunch() {
			this.getSystemInfo().then(() => {
				console.log('init');
				this.init()
			})
			const options = uni.getLaunchOptionsSync();
		},
		onShow() {},
		onHide() {
			console.log('onHide');
		},
		onExit() {
			let messageList = uni.getStorageSync('messageList')
			messageList = messageList ? JSON.parse(messageList) : []
			console.log('exit messageList', messageList);
		},
		methods: {
			// 获取设备信息
			getSystemInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (res) => {
							uni.setStorageSync('systemInfo', res);
							resolve(res); // 成功时返回系统信息
						},
						fail: (err) => {
							reject(err); // 失败时返回错误
						}
					});
				});
			},
			// 初始化
			init() {
				return new Promise((resolve, reject) => {
					appInit().then((res) => {
						uni.setStorageSync('appInfo', JSON.stringify(res.body))
					}).then(() => {
						const userInfo = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')) : {}
						if(Object.keys(userInfo).length === 0) {
							this.login()
						} else {
							this.$isResolve()
						}
					})
				});
			},
			// 微信登录
			login() {
				this.$store.dispatch('loginAction').then(() => {
					this.$isResolve()
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import './tuniao-ui/index.scss';
	@import './tuniao-ui/iconfont.css';
	@import url('/static/css/main.css');

	page {
		height: 100%;
	}
</style>